package sizerecommend

import (
	// "net/http"
	"encoding/json"
	"strconv"
	// "time"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	"oxy_server/model"
	"oxy_server/module/log"
	"oxy_server/res/cmsserver/designplan"
	"oxy_server/until"
)

type (
	SizeRecommend struct {
		ID              bson.ObjectId              `json:"_id" bson:"_id"`
		Kind            string                     `json:"kind" bson:"kind"`
		Gender          string                     `json:"gender" bson:"gender"`
		Version         string                     `json:"version" bson:"version"`
		RecommendedSize []designplan.RecommendSize `json:"recommendsize" bson:"recommendsize"` // 设计组版师 尺码推荐表
	}
	insertSizeRecommend struct {
		Kind            string                     `json:"kind" bson:"kind"`
		Gender          string                     `json:"gender" bson:"gender"`
		Version         string                     `json:"version" bson:"version"`
		RecommendedSize []designplan.RecommendSize `json:"recommendsize" bson:"recommendsize"` // 设计组版师 尺码推荐表
	}
)
type OXYSizeRecommend struct{}

func NewOXYSizeRecommend() *OXYSizeRecommend {
	Data := &OXYSizeRecommend{}
	return Data
}

func (ro *OXYSizeRecommend) GetListHandler(c echo.Context) error {
	log.Info("GetListHandler")
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")

	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "size_recommend_dic"
	M, err := until.JsonToBson(jsonquery)
	if err != nil {
		log.Error("json-> bson 失败", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	query, count, err := model.DB().QuerySort(M, currentpage, perpage, "_id")
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []SizeRecommend{}
	query.All(&ps)
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  ps,
		"count": count,
	})

	return nil

}

func (ro *OXYSizeRecommend) GetInfoHandler(c echo.Context) error {
	log.Info("GetInfoHandler")
	id := c.QueryParam("id")
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "size_recommend_dic"
	query, err := model.DB().QueryOne(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	p := SizeRecommend{}
	query.One(&p)
	str, err := json.Marshal(p)
	if err != nil {
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return nil
	}
	c.JSON(200, map[string]interface{}{
		"msg":  "success",
		"data": string(str),
	})

	return nil
}

func (ro *OXYSizeRecommend) AddHandler(c echo.Context) error {
	s := &insertSizeRecommend{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "size_recommend_dic"
	err := model.DB().Insert(s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil

}
func (ro *OXYSizeRecommend) UpdateHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateSupplierTableHandler", id)

	s := &insertSizeRecommend{}
	if err := c.Bind(s); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "size_recommend_dic"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, s)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}

func (ro *OXYSizeRecommend) DelHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "size_recommend_dic"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil
}
